package c_synchronized;

public class Ticket implements Runnable {
    //总票数100
    private int ticket = 100;

    private Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
               if (ticket > 0) {
                   try {
                       Thread.sleep(100);
                   } catch (InterruptedException e) {
                       throw new RuntimeException(e);
                   }
                   Thread t = Thread.currentThread();
                   String name = t.getName();
                   System.out.println(name + "正在卖票:" + ticket);
                   ticket--;
               } else {
                   break;
               }
           }
        }
    }
}